package com.financial.service.platbalancepay.service.impl;

import com.financial.service.platbalancepay.config.UrlConstants;
import com.financial.service.platbalancepay.connector.PlatformPayFegin;
import com.financial.service.platbalancepay.dao.GybalanceRefundReceiptDao;
import com.financial.service.platbalancepay.domain.GybalanceRefundReceiptDO;
import com.financial.service.platbalancepay.service.BalanceRefundService;
import com.financial.service.platbalancepay.utils.apiresult.ApiResult;
import com.financial.service.platbalancepay.utils.balanceutils.AccessTokenUtil;
import com.financial.service.platbalancepay.utils.balanceutils.HttpClientUtils;
import com.financial.service.platbalancepay.utils.balanceutils.JsonUtils;
import com.financial.service.platbalancepay.utils.balanceutils.RedisUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * describe:
 *
 * @author zhaopengjun
 * @date 2019/9/1 18:49
 */
@Service
public class BalanceRefundServiceImpl implements BalanceRefundService {
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private AccessTokenUtil accessTokenUtil;
    @Autowired
    private HttpClientUtils httpClientUtils;
    @Autowired
    private PlatformPayFegin platformPayFegin;
    @Autowired
    private GybalanceRefundReceiptDao gybalanceRefundReceiptDao;
    public static final Logger logger = LoggerFactory.getLogger(BalanceRefundServiceImpl.class);

    @Override
    public Map<String, Object> refund(Map<String, Object> requestMap) {
        logger.info("financial余额退款接收参数"+ JsonUtils.objectToJson(requestMap));

        String result = null;
        String customerUuid = requestMap.get("customerUuid").toString();
        String payMoney = requestMap.get("payMoney").toString();
        String prePayNo = requestMap.get("prePayNo").toString();

        String platfromMarking = requestMap.get("platfromMarking").toString();
        //TODO 
        //String refundReason = requestMap.get("refundReason").toString();
        if (StringUtils.isEmpty(customerUuid)||StringUtils.isEmpty(payMoney)
                ||StringUtils.isEmpty(prePayNo)){
            return ApiResult.paramIsNull();
        }

        //缓存中获取token
        String accessToken = null;
        Object object = redisUtils.getValue("custumerAccessToken");
        if (null == object) {
            accessToken = accessTokenUtil.getaccessToken();
        } else {
            accessToken = (String) object;
        }
        requestMap.put("accessToken",accessToken);
        try {
            result= httpClientUtils.doPost(UrlConstants.BALANCE_REFUND_URL,requestMap);
            logger.info("++++customerUuid:{},余额退款信息结果:{}",customerUuid,result);
            int platfromId = platformPayFegin.getPlatfromId(platfromMarking);
            //请求够样余额退款回执
            Map<String, Object> resultMap = JsonUtils.jsonToMap(result);
            String returnCode = resultMap.get("return_code").toString();
            String message = resultMap.get("message").toString();
            GybalanceRefundReceiptDO gyRefund = new GybalanceRefundReceiptDO();
            gyRefund.setCustomerUuid(customerUuid);
            gyRefund.setGmtCreate(new Date());
            gyRefund.setGmtModified(new Date());
            gyRefund.setIsDelete("1");
            gyRefund.setOutTradeNo(prePayNo);
            gyRefund.setPlatfromAppid(platfromId);
            gyRefund.setRefundAmount(new BigDecimal(payMoney));
            if(!returnCode.equals("0")){
                gyRefund.setStatus("退款失败");
            }else {
                gyRefund.setStatus("退款成功");
            }
            gyRefund.setReturnMsg(message);
            gybalanceRefundReceiptDao.save(gyRefund);
        } catch (NullPointerException e) {
            e.printStackTrace();
            logger.error("++++customerUuid:{},余额退款保存失败原因:{}",customerUuid,e.getMessage());
        }catch (Exception e3){
            e3.printStackTrace();
            logger.error("++++customerUuid:{},调用够样余额退款失败原因:{}",customerUuid,e3.getMessage());
        }
        return ApiResult.success(result);
    }
}
